import { getCategoryAPI } from '@/apis/category'
import { onMounted, ref } from 'vue'
import { useRoute } from 'vue-router'
import  { onBeforeRouteUpdate } from 'vue-router'

//封装分类数据业务相关代码
export function useCategory() {
    const CategoryData = ref({})
// 获取路由参数
    const route = useRoute()
    const getCategory = async (id = route.params.id) => {
    const res = await getCategoryAPI(id)
        CategoryData.value = res.result
    }
    onMounted(() => {
        getCategory()
    })

    // to 是最新的路由数据
    onBeforeRouteUpdate((to) => {
        getCategory(to.params.id)
    })

    return {
        CategoryData
    }
}